{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: django\n",
      "======================================================================\n",
      "\n",
      "1.  1.  Please edit system and help pages ONLY in the moinmaster wiki!\n",
      "        For more\n",
      "    2.  information, please see MoinMaster:MoinPagesEditorGroup.\n",
      "    3.  master-page:Unknown-Page\n",
      "    4.  master-date:Unknown-Date\n",
      "    5.  acl MoinPagesEditorGroup:read,write,delete,revert All:read\n",
      "\n",
      "Using MatPlotLib to dynamically generate charts in a Django web service\n",
      "-----------------------------------------------------------------------\n",
      "\n",
      "You need to have a working Django installation, plus matplotlib.\n",
      "\n",
      "### Example 1 - PIL Buffer"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# file charts.py\n",
      "def simple(request):\n",
      "    import random\n",
      "    import django\n",
      "    import datetime\n",
      "    \n",
      "    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas\n",
      "    from matplotlib.figure import Figure\n",
      "    from matplotlib.dates import DateFormatter\n",
      "\n",
      "    fig=Figure()\n",
      "    ax=fig.add_subplot(111)\n",
      "    x=[]\n",
      "    y=[]\n",
      "    now=datetime.datetime.now()\n",
      "    delta=datetime.timedelta(days=1)\n",
      "    for i in range(10):\n",
      "        x.append(now)\n",
      "        now+=delta\n",
      "        y.append(random.randint(0, 1000))\n",
      "    ax.plot_date(x, y, '-')\n",
      "    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))\n",
      "    fig.autofmt_xdate()\n",
      "    canvas=FigureCanvas(fig)\n",
      "    response=django.http.HttpResponse(content_type='image/png')\n",
      "    canvas.print_png(response)\n",
      "    return response"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Since some versions of Internet Explorer ignore the content\\_type. The\n",
      "URL should end with \".png\". You can create an entry in your urls.py like\n",
      "this:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "    ...\n",
      "    (r'^charts/simple.png$', 'myapp.views.charts.simple'),\n",
      "    ..."
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}